﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class addOrEditTip : Form
    {
        tipForm parent;
        TextBox txt;
        String val;
        int type;

        public addOrEditTip(tipForm parent, int type)
        {
            this.parent = parent;
            this.type = type;
            InitializeComponent();
            txt = (TextBox)amount.Controls[1];
            txt.TextChanged +=new EventHandler(txt_TextChanged);
            txt.Validating+=new CancelEventHandler(txt_Validating);
            val = txt.Text;

            if (type==1)
            {
                dateTip.Value = DateTime.Parse(parent.getSelectedDate());
                amount.Value = parent.getAmount();
            }
        }

        private void confirmButton_Click(object sender, EventArgs e)
        {
            Tip tip = new Tip();
            tip.setDate(dateTip.Value.ToString("yyyy-MM-dd"));
            tip.setTip((double)amount.Value);
            if (type==0)
            {
                bool success = new TipController().addTip(tip);
                if (success)
                {
                    parent.initializeContent();
                    this.Close();
                }
            }
            else
            {
                DateTime date = DateTime.Parse(parent.getSelectedDate());
                bool success = new TipController().editTip(tip,date.ToString("yyyy-MM-dd"));
                if (success)
                {
                    parent.initializeContent();
                    this.Close();
                }
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            if (txt.Focused)
                val = txt.Text;
        }

        private void txt_Validating(object sender, CancelEventArgs e)
        {
            if (multipleDecPoints(val))
            {
                MessageBox.Show("Invalid input; too many decimal points.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else 
                if(val.Equals("."))
                {
                    MessageBox.Show("Invalid input.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
                else
                {
                    double check = Double.Parse(val);
                    if (check < (double)amount.Minimum || check > (double)amount.Maximum)
                        MessageBox.Show("Invalid input; out of range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        }

        private bool multipleDecPoints(String num)
        {
            int nPoints=0;
            int i=0;
            while (nPoints < 2 && i < num.Length)
            {
                if (num[i] == '.')
                    nPoints++;
                i++;
            }
            if (nPoints < 2)
                return false;
            else
                return true;
        }


    }
}
